package com.xavier.book.spring_book_demo.model;

import com.xavier.book.spring_book_demo.enums.ResultCodeEnum;
import lombok.Data;

@Data
public class Result<T> {
    private ResultCodeEnum code;//-1 未登录，200 正常，-2 出错
    private String errMsg;
    private T data;

    public static <T> Result<T> sucess(T data) {
        Result<T> result = new Result<>();
        result.setCode(ResultCodeEnum.SUCCESS);
        result.setErrMsg("");
        result.setData(data);
        return result;
    }

    public static <T> Result<T> fail (String errMsg) {
        Result<T> result = new Result<>();
        result.setCode(ResultCodeEnum.FAIL);
        result.setErrMsg(errMsg);
        result.setData(null);
        return result;
    }

    public static <T> Result<T> fail (String errMsg,T data) {
        Result<T> result = new Result<>();
        result.setCode(ResultCodeEnum.FAIL);
        result.setErrMsg(errMsg);
        result.setData(data);
        return result;
    }

    public static <T> Result<T> unlogin() {
        Result<T> result = new Result<>();
        result.setCode(ResultCodeEnum.UNLOGIN);
        result.setErrMsg("用户未登录");
        return result;
    }
}
